# app/core/error_codes.py
from enum import Enum
from typing import Dict, Any


class ErrorCode(Enum):
    """错误码枚举"""
    # 通用错误
    SUCCESS = (0, "成功")
    UNKNOWN_ERROR = (1000, "未知错误")
    VALIDATION_ERROR = (1001, "参数验证失败")
    DATABASE_ERROR = (1002, "数据库操作失败")
    LIFE_SPAN_ERROR = (1003, "lifespan初始化失败")

    # 业务错误 - 机器人相关
    ROBOT_CONNECT_FAIL = (2000, "机器连接失败")
    ROBOT_NOT_FOUND = (2001, "机器人未找到")
    ROBOT_OFFLINE = (2002, "机器人离线")
    ROBOT_BUSY = (2003, "机器人忙碌中")
    SCAN_ROBOT_FAILED = (2004, "扫描机器失败")
    GET_GATEWAY_FAILED = (2005, "获取默认网关失败")
    SCAN_IP_FAILED = (2006, "扫描IP失败")
    GET_HEALTH_FAILED = (2007, "扫描Flex获取信息失败")
    ROBOT_DISCONNECT_FAIL = (2008, "关闭连接失败")

    # 业务错误 - 测试数据相关
    TEST_DATA_NOT_FOUND = (3001, "测试数据未找到")
    TEST_DATA_GENERATION_FAILED = (3002, "测试数据生成失败")
    DOWNLOAD_FAILED = (3003, "文件下载失败")
    MOCK_DATA_ERROR = (3004, "Mock Data Error")

    # 权限错误
    UNAUTHORIZED = (4001, "未授权访问")
    PERMISSION_DENIED = (4002, "权限不足")
    TOKEN_EXPIRED = (4003, "令牌已过期")

    # driver错误
    PARAMIKO_EXECUTE_FAIL = (5000, "执行命令失败")
    # http client
    CREATE_RUN_FAIL = (6000, "创建run id失败")
    FLEX_HTTP_EXCEPTION = (6001, "Flex Http 通信状态码有误")

    def __init__(self, code: int, message: str):
        self.code = code
        self.message = message

    @property
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            "code": self.code,
            "message": self.message
        }


# 错误码映射
ERROR_CODE_MAP = {error.code: error for error in ErrorCode}


def get_error_message(code: int) -> str:
    """根据错误码获取错误信息"""
    error = ERROR_CODE_MAP.get(code)
    return error.message if error else "未知错误"
